package com.moonciki.artadmin.core.utils;

import com.alibaba.fastjson.JSON;
import com.moonciki.artadmin.core.enums.ResponseEnum;
import com.moonciki.artadmin.core.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;

@Slf4j
public class RestUtil {

    public static void responseError(ResponseEntity entity, String msg){
        try {
            int codeValue = entity.getStatusCodeValue();

            Object respBody = entity.getBody();

            String respJson = JSON.toJSONString(respBody);

            log.error("### Rest Response Error: [{}] => {}", codeValue, respJson);
        } catch (Exception e) {
            log.error("### Error: ", e);
        }

        if(StringUtils.isNotBlank(msg)) {
            throw CustomException.createException(ResponseEnum.rest_error.info(msg));
        }else{
            throw CustomException.createException(ResponseEnum.rest_error.info());
        }
    }

}
